package com.lcg.exchange.security;

import com.lcg.exchange.model.Role;
import com.lcg.exchange.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
public class MyUserDetails extends User implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;
    private List<Role> roleList = null;
    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public MyUserDetails(User user,List<Role> roles){
        super(user);
        this.roleList = roles;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        /*StringBuilder authoritiesBuilder = new StringBuilder("");
        List<Role> tempRoleList = this.getRoleList();
        if (null != tempRoleList) {
            for (Role role : tempRoleList) {
                authoritiesBuilder.append(",").append(role.getName());
            }
        }
        String authoritiesStr = "";
        if(authoritiesBuilder.length()>0) {
            authoritiesStr = authoritiesBuilder.deleteCharAt(0).toString();
        }
        log.info("VUserDetails getAuthorities [authoritiesStr={} ", authoritiesStr);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authoritiesStr);*/
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (Role role:roleList
             ) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return super.getUserPassword();
    }

    @Override
    public String getUsername() {
        return super.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }
}
